package org.rsbot.script.methods;

import java.util.HashSet;
import java.util.Set;
import org.rsbot.client.Client;
import org.rsbot.client.Node;
import org.rsbot.client.RSNPCNode;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSNPC;

public class NPCs extends MethodProvider
{
  public static final Filter<RSNPC> ALL_FILTER = new Filter() {
    public boolean accept(RSNPC paramRSNPC) {
      return true;
    }
  };

  NPCs(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public RSNPC[] getAll()
  {
    return getAll(ALL_FILTER);
  }

  public RSNPC[] getAll(Filter<RSNPC> paramFilter)
  {
    int[] arrayOfInt1 = this.methods.client.getRSNPCIndexArray();
    HashSet localHashSet = new HashSet();
    for (int k : arrayOfInt1) {
      Node localNode = this.methods.nodes.lookup(this.methods.client.getRSNPCNC(), k);
      if ((localNode instanceof RSNPCNode)) {
        RSNPC localRSNPC = new RSNPC(this.methods, ((RSNPCNode)localNode).getRSNPC());
        if (paramFilter.accept(localRSNPC)) {
          localHashSet.add(localRSNPC);
        }
      }
    }
    return (RSNPC[])localHashSet.toArray(new RSNPC[localHashSet.size()]);
  }

  public RSNPC getNearest(Filter<RSNPC> paramFilter)
  {
    int i = 20;
    Object localObject = null;
    int[] arrayOfInt1 = this.methods.client.getRSNPCIndexArray();

    for (int m : arrayOfInt1) {
      Node localNode = this.methods.nodes.lookup(this.methods.client.getRSNPCNC(), m);
      if ((localNode instanceof RSNPCNode)) {
        RSNPC localRSNPC = new RSNPC(this.methods, ((RSNPCNode)localNode).getRSNPC());
        if (paramFilter.accept(localRSNPC)) {
          int n = this.methods.calc.distanceTo(localRSNPC);
          if (n < i) {
            i = n;
            localObject = localRSNPC;
          }
        }
      }
    }
    return localObject;
  }

  public RSNPC getNearest(int[] paramArrayOfInt)
  {
    return getNearest(new Filter(paramArrayOfInt) {
      public boolean accept(RSNPC paramRSNPC) {
        for (int k : this.val$ids) {
          if (paramRSNPC.getID() == k) {
            return true;
          }
        }
        return false;
      }
    });
  }

  public RSNPC getNearest(String[] paramArrayOfString)
  {
    return getNearest(new Filter(paramArrayOfString) {
      public boolean accept(RSNPC paramRSNPC) {
        for (String str : this.val$names) {
          if (paramRSNPC.getName().equals(str)) {
            return true;
          }
        }
        return false;
      }
    });
  }
}